home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / cmdln.zip / CMDLN.INT < prev    next >
Text File  |  1993-01-04  |  5KB  |  163 lines

  1. {
  2.  
  3.     cmdln.c
  4.     6-4-90
  5.     DOS Command line option parser
  6.  
  7.     Copyright 1990
  8.     John W. Small
  9.     All rights reserved
  10.  
  11.     PSW / Power SoftWare
  12.     P.O. Box 10072
  13.     McLean, VA 22102 8072
  14.     (703) 759-3838
  15.  
  16.  
  17.     Notes:
  18.  
  19.     1.  Call CmdLnParams.init() with the options string to
  20.     parse command line arguments.  The options string is a
  21.     string of the option characters that may appear on your
  22.     programs command line after the DOS switch character,
  23.     '/' or '-'.  If an option takes an argument then a colon
  24.     must immediately follow that option character in the
  25.     options string to let CmdLnParams.getOption know to look
  26.     for the argument.  The syntax for the option string is
  27.     as follows:
  28.  
  29.         options string ::= (optch[:])*
  30.  
  31.     Your read the syntax as: An options string is zero or
  32.     more (the "*" indicates this) option characters any of
  33.     which may be followed by a colon to indicate that the
  34.     option takes an argument.
  35.  
  36.  
  37.     2. GetOption returns the current option character being
  38.     processed along with any argument in the object variable
  39.     optArg.  OptArg is valid only on options requiring
  40.     arguments.  If the argument is missing then optArg = ''.
  41.     If the current option character being processed is
  42.     unrecognized, i.e. not in the options string passed to
  43.     init(), then getOption returns '?' with the
  44.     unrecognized character stored in the object variable,
  45.     optNot.  GetOption returns #0 when there are no more
  46.     options to process.  The object variable, paramNo, is
  47.     the index into ParamStr() of the first non-switch
  48.     command line parameter.  The object variable, optCh,
  49.     maintains a copy of the latest value returned by
  50.     getOption.
  51.  
  52.  
  53.     3.  When your program is invoked, getOption recognizes
  54.     clusters of command line options.  A cluster is the
  55.     switch character followed immediately by any number of
  56.     option characters, no white space.  The options clusters
  57.     must preceed any other command line parameters since
  58.     getOption stops processing on the first parameter that
  59.     is not a switch.  If an option takes an argument then
  60.     the option's character must be the last option in the
  61.     cluster with the argument immediately following or
  62.     separated by white space.  The argument must not have
  63.     any white space, though it may contain the switch
  64.     character.
  65.  
  66.         command line option cluster ::=
  67.  
  68.         ('/'|'-')([optch]*argch[whitespace]argument)|optch+
  69.  
  70.     Wow! That reads: a command line option cluster starts
  71.     with the switch character ('/' or '-' in DOS) with one
  72.     or more option characters (+ means one or more) or
  73.     (| means or) any number of option characters, only the
  74.     last of which is allowed to take an argument.  The
  75.     argument can be either tacked on to the end of the
  76.     option cluster or stand off by itself.  In either case,
  77.     argument contains no white space!  If a switch character
  78.     appears in a cluster by itself or if two switch
  79.     characters lead off a cluster then option parsing is
  80.     terminated and the next parameter starts the non switched
  81.     arguments.  This allows the first non switched argument
  82.     to start with the switch character, i.e. let the
  83.     preceeding cluster be either a single switch character
  84.     or lead off with two switch characters.
  85.  
  86.  
  87.     4.  For example, if init is called with the options
  88.     string, 'C:af:z', then 'C' and 'f' take arguments.  A
  89.     valid command line would be:
  90.  
  91.         fake /afnew /zC cmdfile outfile
  92.  
  93.     with repeated calls to getOption returning:
  94.  
  95.         'a'
  96.         'f' with optArg  = 'new'
  97.         'z'
  98.         'C' with optArg  = 'cmdfile'
  99.         #0  with paramNo = 4
  100.  
  101.  
  102.  
  103.     5.  After getOption has processed all options in the
  104.     command line it will return #0.  Now your application
  105.     can pick up the rest of the command line parameters
  106.     using the object variable, paramNo, to index ParamStr()
  107.     for the first non switch parameter. Okay let's see all
  108.     this in action!
  109.  
  110.  
  111.     program fake;
  112.     uses cmdln;
  113.     var params : CmdLnParams;
  114.         ofile : string;
  115.     begin
  116.         params.init('C:af:z');
  117.         while (params.getOption <> #0) do
  118.             case (params.optCh) of
  119.                 'C': if length(params.optArg) > 0 then
  120.                         // process 'C' option
  121.                     else
  122.                         writeln('Option C: missing argument');
  123.                 'a': // process 'a' option
  124.                 'f': if length(params.optArg) > 0 then
  125.                         // process 'f' option
  126.                     else
  127.                         writeln('Option f: missing argument');
  128.                 'z': // process 'z' option
  129.             end;
  130.         if (params.paramNo < ParamCount) then
  131.             ofile := ParamStr(params.ParamNo);
  132.         ...
  133.     end.
  134.  
  135.  
  136.     Besure to run and study cmdln.dem for a real example of
  137.     using init(), getOption, nextCluster, and paramNo.
  138.  
  139. }
  140.  
  141.  
  142. unit cmdln;
  143.  
  144. interface
  145.  
  146.     type
  147.  
  148.         CmdLnParams = object
  149.         { private: }
  150.             options,
  151.             cluster : string;
  152.             option  : byte;
  153.             optEnd  : boolean;
  154.         { public: }
  155.             optCh,
  156.             optNot  : char;
  157.             optArg  : string;
  158.             paramNo : word;
  159.             constructor init(opts : string);
  160.             function  getOption : char;
  161.             procedure nextCluster;
  162.         end;
  163.